package com.findtech.threePomelos.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.findtech.threePomelos.activity.MyDeviceOperateActivity;
import com.findtech.threePomelos.base.MainActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.activity.HomeActivity;
import com.findtech.threePomelos.bluetooth.BLEDevice;
import com.findtech.threePomelos.bluetooth.CubicBLEDevice;
import com.findtech.threePomelos.entity.TravelInfoEntity;
import com.findtech.threePomelos.net.NetWorkRequest;
import com.findtech.threePomelos.net.QueryBabyInfoCallBack;
import com.findtech.threePomelos.service.RFStarBLEService;
import com.findtech.threePomelos.utils.RequestUtils;

public class BindCarButtonFragment extends Fragment implements View.OnClickListener,
        BLEDevice.RFStarBLEBroadcastReceiver {
    private Context mContext;
    private TextView bindCar, noBindCar;
    private MyApplication app = null;
    private NetWorkRequest netWorkRequest;
    private String mFrom;

    public BindCarButtonFragment() {
        mContext = getContext();
    }

    public BindCarButtonFragment(Context context) {
        mContext = context;
    }
    public BindCarButtonFragment(Context context, String from) {
        mContext = context;
        mFrom = from;
        Log.d("ZZ", "mFrom = " + mFrom);
    }

    @SuppressWarnings("static-access")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (mContext != null) {
            app = (MyApplication) mContext.getApplicationContext();
        } else {
            app = MyApplication.getInstance();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(MyApplication.TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_bind_car, container, false);
        this.initView(view);
        netWorkRequest = new NetWorkRequest(mContext);
        return view;
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.d(MyApplication.TAG, "onResume");
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
//        app.manager.cubicBLEDevice.ungisterReceiver();
        super.onPause();
    }

    @SuppressWarnings("static-access")
    protected void initView(View view) {
        bindCar = (TextView) view.findViewById(R.id.bind_car);
        noBindCar = (TextView) view.findViewById(R.id.no_bind_car);
        bindCar.setOnClickListener(this);
        noBindCar.setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onReceive(Context context, Intent intent, String macData, String uuid) {
        try {
            String action = intent.getAction();
            if (RFStarBLEService.ACTION_GATT_CONNECTED.equals(action)) {
                Log.d(MyApplication.TAG, "SearchFragment --> 连接完成");
            } else if (RFStarBLEService.ACTION_GATT_DISCONNECTED.equals(action)) {
                Log.d(MyApplication.TAG, "SearchFragment --> 连接断开");
            } else if (RFStarBLEService.ACTION_GATT_SERVICES_DISCOVERED
                    .equals(action)) {
                String deviceNum = RequestUtils.getSharepreference(mContext).getString(RequestUtils.DEVICE, "");
                netWorkRequest.saveBlueToothIsBind(true, deviceNum, new QueryBabyInfoCallBack.SaveIsBind() {

                    @Override
                    public void finishSaveIsBindSuccess(boolean isBind, String deviceId) {
                        if (BindCarButtonFragment.this.isVisible()) {
                            startActivity(new Intent(mContext, HomeActivity.class));
                        }
                    }

                    @Override
                    public void finishSaveIsBindFail() {
                        if (BindCarButtonFragment.this.isVisible()) {
                            startActivity(new Intent(mContext, HomeActivity.class));
                        }
                    }
                });//上传服务器，绑定和蓝牙地址
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onReceiveDataAvailable(String dataType, String data, TravelInfoEntity travelInfoEntity, String time) {

    }

    @Override
    public void onClick(View view) {
        if (view == bindCar) {
            app.manager.isEnabled((Activity) mContext);
            Log.d("ZZ", "app.manager.cubicBLEDevice = " + app.manager.cubicBLEDevice);
            app.manager.cubicBLEDevice = new CubicBLEDevice(
                    mContext.getApplicationContext(), app.manager.bluetoothDevice);
            app.manager.cubicBLEDevice.setBLEBroadcastDelegate(this);
            app.manager.startScanBluetoothDevice();
        }
        if (view == noBindCar) {
            netWorkRequest.saveBlueToothIsBind(false, "", new QueryBabyInfoCallBack.SaveIsBind() {
                @Override
                public void finishSaveIsBindSuccess(boolean isBind, String deviceId) {
                    startActivity(new Intent(mContext, HomeActivity.class));
                }

                @Override
                public void finishSaveIsBindFail() {
                    startActivity(new Intent(mContext, HomeActivity.class));
                }
            });//上传服务器，不绑定和蓝牙地址为空

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
